package com.wmlive.hhvideo.heihei.recordvideo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.dyuiapi.api.APICallback;
import com.dyuiapi.api.DyUIAPI;
import com.dyuiapi.api.model.MusicInfo;
import com.dyuiapi.api.model.ShortVideoInfo;
import com.wmlive.hhvideo.common.AppCacheFileUtils;
import com.wmlive.hhvideo.common.GlobalParams;
import com.wmlive.hhvideo.common.MyAppActivityManager;
import com.wmlive.hhvideo.heihei.discovery.activity.SearchMusicVideoActivity;
import com.wmlive.hhvideo.heihei.recordvideo.utils.Utils;

import java.io.File;

import cn.wmlive.hhvideo.rxbuslib.RxBus2;

/**
 * 回调接口实现
 */
public class DyUIAPICallbackImpl implements APICallback {
    private static DyUIAPICallbackImpl mInstance;

    private ProgressDialog mpDialog;
    private Context mContext;
    private ShortVideoInfo mCurrentShorVideoInfo;//当前操作的视频信息
    private Handler mCurrentCallbackHandler;//用于回调的handler
    private String strShorVideoFirstClipImagPath;//视频制定截图文件夹
    private String strCurrentLocalImagPath;//视频制定截图文件地址


    protected OnExportListener mOnExportListener;

    //加载监听接口
    public static interface OnExportListener {
        public void onExportStart(ShortVideoInfo shortVideoInfo);

        public void onExporting(int progress, int max);

        public void onExportEnd(String exportedVideoPath, boolean exportSuccess);
    }

    /**
     * 获取单例
     *
     * @return
     */
    public static DyUIAPICallbackImpl getInstance() {
        if (null == mInstance) {
            synchronized (DyUIAPICallbackImpl.class) {
                if (null == mInstance) {
                    mInstance = new DyUIAPICallbackImpl();
                }
            }
        }
        return mInstance;
    }

    private DyUIAPICallbackImpl() {
    }


    public void setContext(Context context) {
        mContext = context;
        strShorVideoFirstClipImagPath = AppCacheFileUtils.getShorVideoFirstClipImgPath(mContext) + File.separator;
    }

    //设置回调handler
    public void setmCurrentCallbackHandler(Handler mCurrentCallbackHandler) {
        this.mCurrentCallbackHandler = mCurrentCallbackHandler;
    }

    /**
     * 响应需要切换配乐
     *
     * @param activity    回调的所在activity
     * @param requestCode 请求code
     */
    @Override
    public void onChangeMusic(Activity activity, int requestCode) {
        SearchMusicVideoActivity.startfromDyUIAPI(activity, requestCode);
    }

    /**
     * 导出开始
     *
     * @param shortVideoInfo 导出时
     */
    @Override
    public void onExportStart(ShortVideoInfo shortVideoInfo) {
        //结束mainActivity 之前的activity
        MyAppActivityManager.getInstance().finishAllActivityBefore(com.wmlive.hhvideo.heihei.mainhome.activity.MainNewActivity.class);
        //显示home
        RxBus2.get().send(GlobalParams.EventType.TYPE_SHOW_MAIN_FIRST, true);

        strCurrentLocalImagPath = strShorVideoFirstClipImagPath + Utils.getCurrentTimeMillis() + ".jpg";
        //获取缩率图
        DyUIAPI.getSnapShot(shortVideoInfo, strCurrentLocalImagPath, GlobalParams.Config.SHORVIDEO_FIRST_CLIP_SECOND);

        if (mOnExportListener != null) {
            mOnExportListener.onExportStart(shortVideoInfo);
        } else if (mCurrentCallbackHandler != null && shortVideoInfo != null) {
            Message msg = mCurrentCallbackHandler.obtainMessage();
            msg.what = 1;
            Bundle mBundle = new Bundle();
            mBundle.putString("title", shortVideoInfo.getTitle());
            long  strTopicId = 0;
            String strAt_users_id = "";
            if (shortVideoInfo.getExtraInfo() != null) {
                UserShorVideoInfoExtraInfo userShorVideoInfoExtraInfo = (UserShorVideoInfoExtraInfo) shortVideoInfo.getExtraInfo();
                if (userShorVideoInfoExtraInfo != null && userShorVideoInfoExtraInfo.getTopicInfo() != null) {
                    strTopicId = userShorVideoInfoExtraInfo.getTopicInfo().getmTopicId();
                }
                if (userShorVideoInfoExtraInfo != null && !TextUtils.isEmpty(userShorVideoInfoExtraInfo.getAt_user_ids())) {
                    strAt_users_id = userShorVideoInfoExtraInfo.getAt_user_ids();
                }
            }
            long  iMusicId = 0l;
            if (shortVideoInfo.getMusicInfo() != null) {
                MusicInfo musicInfo = shortVideoInfo.getMusicInfo();
                if (musicInfo != null && musicInfo.getExtraInfo() != null) {
                    MusicCustomExtraInfo userMusicInfoExtraInfo = (MusicCustomExtraInfo) musicInfo.getExtraInfo();
                    iMusicId = userMusicInfoExtraInfo.getMusicId();
                }
            }

            mBundle.putLong("topic_id", strTopicId);
            mBundle.putLong("music_id", iMusicId);
            mBundle.putString("at_user_ids", strAt_users_id);
            mBundle.putString("shorvideo_first_image", strCurrentLocalImagPath);
            mBundle.putFloat("opus_length", shortVideoInfo.getDuration());
            msg.setData(mBundle);
            mCurrentCallbackHandler.sendMessage(msg);
        } else {
        }
        mCurrentShorVideoInfo = shortVideoInfo;
    }

    /**
     * 导出正在进行中的
     *
     * @param progress 当前进度
     * @param max      进度最大值
     */
    @Override
    public void onExporting(int progress, int max) {
//        if (mpDialog.isShowing()) {
//            mpDialog.setMessage("" + (progress / 10) + "%");
//        }
        if (mOnExportListener != null) {
            mOnExportListener.onExporting(progress, max);
        } else if (mCurrentCallbackHandler != null) {
            Message msg = mCurrentCallbackHandler.obtainMessage();
            msg.what = 2;
            msg.arg1 = (progress / 10);
            mCurrentCallbackHandler.sendMessage(msg);
        }
    }

    /**
     * 导出完成
     *
     * @param exportedVideoPath 导出后的视频路径
     * @param exportSuccess     为true代表导出成功
     */
    @Override
    public void onExportEnd(String exportedVideoPath, boolean exportSuccess) {
        if (exportSuccess) {
            //获取缩略图
            strCurrentLocalImagPath = strShorVideoFirstClipImagPath + Utils.getCurrentTimeMillis() + ".jpg";
            DyUIAPI.getSnapShot(exportedVideoPath, strCurrentLocalImagPath, GlobalParams.Config.SHORVIDEO_FIRST_CLIP_SECOND);
            if (mOnExportListener != null) {
                mOnExportListener.onExportEnd(exportedVideoPath, exportSuccess);
            } else if (mCurrentCallbackHandler != null) {
                Message msg = mCurrentCallbackHandler.obtainMessage();
                msg.what = 3;
                Bundle mBundle = new Bundle();
                mBundle.putString("UploadPath", exportedVideoPath);
                msg.setData(mBundle);
                mCurrentCallbackHandler.sendMessage(msg);
            }
        } else {
            //导出失败
            Message msg = mCurrentCallbackHandler.obtainMessage();
            msg.what = 0;
            mCurrentCallbackHandler.sendMessage(msg);
        }


//        if (exportSuccess) {
//            MainActivity.mDraftBox.onDataRefresh();
//            Intent intent = new Intent();
//            intent.setAction(Intent.ACTION_VIEW);
//            String type = "video/*";
//            Uri uri = Uri.parse(exportedVideoPath);
//            intent.setDataAndType(uri, type);
//            mContext.startActivity(intent);
//            /**
//             * TODO:
//             * 发布时会先保存到草稿箱
//             * 导出成功并且上传成功，需删除已发布视频信息，上传失败时，不调用以下代码，保留当前视频信息
//             */
//            mShortVideoInfo.delete();
//        }
    }

    /**
     * 删除草稿箱中制定的视频
     */
    public void deletDraftBoxVideoInfo() {
        if (mCurrentShorVideoInfo != null) {
            mCurrentShorVideoInfo.delete();
        }
    }

    /**
     * 获取所楼图
     */
    public String getShortVideoFirstClipImgPath() {
        return strCurrentLocalImagPath;

    }
}